/**
 * chinagoods.com
 * Copyright (c) 2020-2024 All Rights Reserved.
 */
package com.ruoyi.system.biz.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.enums.OrderStatusEnum;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.system.biz.MerchantCountBizService;
import com.ruoyi.system.domain.CashHistory;
import com.ruoyi.system.domain.MerchantCount;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.service.CashHistoryService;
import com.ruoyi.system.service.MerchantCountService;
import com.ruoyi.system.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author yangpeng
 * Created by on 2024-01-30 13:04
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class MerchantCountBizServiceImpl extends BaseBizServiceImpl implements MerchantCountBizService {

    private final MerchantCountService userCountService;

    private final OrderService orderService;
    private final CashHistoryService cashHistoryService;

    @Override
    public void addUserCount() {
        DateTime end = DateUtil.beginOfDay(new Date());
        DateTime start = DateUtil.offsetDay(end, -1);
        addUserCount(start, end);
    }

    @Override
    public void addUserCountForMonth() {
        DateTime start = DateUtil.beginOfMonth(new Date());
        DateTime end = DateUtil.offsetDay(start, 1);
        DateTime dateTime = DateUtil.beginOfDay(new Date());
        while (true) {
            if (start.getTime() == dateTime.getTime()) {
                log.info("现在:{}", start);
                break;
            }
            addUserCount(start, end);
            start = DateUtil.offsetDay(start, 1);
            end = DateUtil.offsetDay(end, 1);
        }

    }

    //统计昨天的数据
    @Override
    public void addUserCount(Date start, Date end) {
        List<Long> userIds = orderService.selectMerchantIds(start, end);
        if (CollectionUtil.isEmpty(userIds)) {
            return;
        }
        for (int i = 0; i < userIds.size(); i++) {
            BigDecimal earning = BigDecimal.ZERO;
            Long userId = userIds.get(i);
            Order order = new Order();
            order.setMerchantId(userId);
            order.setOrderStatus(OrderStatusEnum.success.getValue());
            PageDomain pageDomain = new PageDomain();
            pageDomain.setPageSize(1);
            pageDomain.setBeginTime(DateUtil.formatDateTime(start));
            pageDomain.setEndTime(DateUtil.formatDateTime(end));

            CashHistory cashHistory = new CashHistory();
            cashHistory.setUserId(userId);

            List<CashHistory> list = cashHistoryService.findList(cashHistory, pageDomain);
            if (CollectionUtil.isNotEmpty(list)) {
                earning = list.get(0).getEarning();
            }
            //统计成功订单数
            int orderNum = orderService.countByDomain(order, start, end, "sendTime");


            //统计订单金额
            BigDecimal orderAmount = orderService.sum("payAmount", order, start, end, "sendTime");
            cashHistory.setChangeType("reduce");
            BigDecimal sum = cashHistoryService.sum("changeMoney", cashHistory, start, end);
            //统计订单总数
            order.setOrderStatus(null);
            int totalNum = orderService.countByDomain(order, start, end);
            //统计订单总金额
            BigDecimal totalAmount = orderService.sum("payAmount", order, start, end);

            //组装数据并插入
            MerchantCount userCount = new MerchantCount();
            userCount.setUserId(userId);
            userCount.setFinishAmount(orderAmount);
            userCount.setFinishNum(orderNum);
            userCount.setWithdraw(sum);
            userCount.setCountDate(DateUtil.formatDate(start));
            userCount.setEarning(earning);
            userCount.setTotalNum(totalNum);
            userCount.setTotalAmount(totalAmount);
            if (totalNum != 0) {
                double rate = 100 * (double)orderNum / totalNum;
                BigDecimal bigDecimal = NumberUtil.toBigDecimal(rate);

                userCount.setRate(BigDecimalUtil.setDecimal(bigDecimal));
            }

            userCountService.addDomain(userCount);
        }

    }

    public static void main(String[] args) {
        int orderNum=24;
        int totalNum=43;
        double rate = 100 *(double) orderNum / totalNum;
        System.out.println(rate);
    }


}